/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.ListCell;
import org.aswing.ListCellFactory;

import com.bourre.core.HashCodeFactory;
import com.bourre.utils.ClassUtils;

import fever.utils.Stringifier;

/**
 * Cell renderer factory for JList.
 * 
 * <p>Example 
 * {@code
 *   var itemDelete : FvContextMenuItem = new FvContextMenuItem( "delete" );
 *   var itemDisplay : FvContextMenuItem = new FvContextMenuItem( "show" );
 *  
 *   var list : JList = new JList( 
 * 
 *  	["male", "female", "others ?"], 
 *  	
 *  	new FvListCellFactory ( 
 *  		new FvDefaultContextMenuCell(), 
 *  		[ itemDelete, itemDisplay ]
 *  	)
 *  	
 *   );
 *  
 *   list.addEventListener( FvContextMenuCellEvent.onContextCellItemEVENT, _onListClick, this );
 * }
 * 
 * @author Romain Ecarnot
 */
class fvaswing.cells.FvListCellFactory implements ListCellFactory
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _shareCelles : Boolean;
	private var _cellHeight : Number;
	private var _template : String;
	private var _templateArgs : Array;
	private var _useSameHeight : Boolean;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
		
	/**
	 * Constructor.
	 * 
	 * @param shareCelles is share cells for list items.
	 */
	public function FvListCellFactory( template : ListCell, height : Number  )
	{
		_shareCelles = false;
		_cellHeight = height;
		
		_template = ClassUtils.getFullyQualifiedClassName( template );
		_templateArgs = arguments.splice( 2 );
		
		_useSameHeight = true;
	}
	
	/**
	 * Creates a new list cell.
	 */
	public function createNewCell() : ListCell
	{
		return HashCodeFactory.buildInstance( _template, _templateArgs );	}
	
	/**
	 * Defines if all cells are the same height {@code true} or not 
	 * {@code false}.
	 */
	public function setAllCellHasSameHeight( b : Boolean ) : Void
	{
		_useSameHeight = b;
	}
	
	/**
	 * Returns {@code true} if all cells have the same height.
	 */
	public function isAllCellHasSameHeight() : Boolean
	{
		return _useSameHeight;
	}
	
	/**
	 * Sets share cells status.
	 */
	public function setShareCells( enabled : Boolean ) : Void
	{
		_shareCelles = enabled;	
	}
	
	/**
	 * Returns share cells status.
	 */
	public function isShareCells() : Boolean
	{
		return _shareCelles;
	}
	
	/**
	 * Returns the height for all cells.
	 */
	public function getCellHeight() : Number
	{
		if( _cellHeight < 0 )
		{
			var cell : ListCell = createNewCell();
			cell.setCellValue("JjHhWpqQ1@|");
			_cellHeight = cell.getCellComponent().getPreferredSize().height;
		}
		
		return _cellHeight;
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
}